﻿using Fiddler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ProgramCat.FiddlerExtension
{
    public class GenerateCodeHelp
    {
        public static void GenerateHeadAndCookies_Jy(Session session, ref StringBuilder stringBuilder, int compilingMode)
        {

            if (stringBuilder == null)
            {
                return;
            }
            string cookies = "";
            if (compilingMode == 0) //命令易读方式
            {
                foreach (var item in session.RequestHeaders)
                {
                    switch (item.Name.ToLower())
                    {
                        case "cookie":
                            cookies = item.Value;
                            break;
                        case "accept-encoding":
                            stringBuilder.AppendLine(string.Format("'ADD_协议头.添加 (\"{0}\",\"{1}\"）' 压缩头 一般不用设置",
                               item.Name,
                               item.Value));
                            break;
                        case "content-length":
                            break;
                        default:
                            //ADD_协议头.添加 ("",""）
                            stringBuilder.AppendLine(string.Format("ADD_协议头.添加 (\"{0}\",\"{1}\"）",
                               item.Name,
                               item.Value));
                            break;
                    }
                }
                stringBuilder.AppendLine(string.Format("局_提交协议头 = ADD_协议头.获取协议头数据()"));
                
                if (cookies != "")
                {
                    stringBuilder.AppendLine(string.Format("局_提交cookie = \"{0}\"", cookies));
                }
                return;
            }
            int numberTemp = session.RequestHeaders.Count();
            //纯文本方式
            stringBuilder.Append("局_提交协议头 = ");
            foreach (var item in session.RequestHeaders)
            {
                numberTemp--;
                switch (item.Name.ToLower())
                {
                    case "cookie":
                        cookies = item.Value;
                        break;
                    case "content-length":
                        break;
                    default:
                        //ADD_协议头.添加 ("",""）
                        if (numberTemp == 0)
                        {
                            stringBuilder.Append(string.Format("\"{0}:{1}\"", item.Name, item.Value));
                        }
                        else
                        {
                            stringBuilder.Append(string.Format("\"{0}:{1}\" ＋ #换行符 ＋ ", item.Name, item.Value));
                        }
                        break;
                }
            }
            string endStr = "＋ #换行符 ＋ ";
            if (stringBuilder.Length > endStr.Length)
            {
                char[] copyTemp = new char[endStr.Length];
                int index = stringBuilder.Length - endStr.Length;
                stringBuilder.CopyTo(index, copyTemp, 0, endStr.Length);
                if (new String(copyTemp) == endStr)
                {
                    stringBuilder.Remove(index, endStr.Length);
                }
            }

            stringBuilder.Append("\r\n");
            if (cookies != "")
            {
                stringBuilder.AppendLine(string.Format("局_提交cookie = \"{0}\"", cookies));
            }
        }

        public static void GenerateHeadAndCookies_Yc(Session session, ref StringBuilder stringBuilder, int compilingMode)
        {

            if (stringBuilder == null)
            {
                return;
            }
            string cookies = "";
            if (compilingMode == 0) //命令易读方式
            {
                foreach (var item in session.RequestHeaders)
                {
                    switch (item.Name.ToLower())
                    {
                        case "cookie":
                            cookies = item.Value;
                            break;
                        case "accept-encoding":
                            stringBuilder.AppendLine(string.Format("'http.SetRequestHeader (\"{0}\",\"{1}\"）",
                              item.Name,
                              item.Value));
                            break;
                        case "content-length":
                            break;
                        default:
                            stringBuilder.AppendLine(string.Format("http.SetRequestHeader (\"{0}\",\"{1}\"）",
                              item.Name,
                              item.Value));
                            break;
                    }
                }
                if (cookies != "")
                {
                    stringBuilder.AppendLine(string.Format("局_提交cookie = \"{0}\"", cookies));
                }
                return;
            }
        }
    }
}

